package insight.Builder;

/**
 * @author huu
 * @create 2025-08-26-14:46
 */
public class User {
    private String name;
    private int age;

    //不提供构造函数
    private User(Builder builder) {
        this.name = builder.name;
        this.age = builder.age;
    }
    public String getName() {
        return name;
    }
    public int getAge() {
        return age;
    }

    //User user = User.builder()
    //                .name("huu")
    //                .age(18)
    //                .build();

    public static Builder builder(){
        return new Builder();
    }

    public static class Builder {
        private  String name;
        private  int age;
        private Builder(){};
        public Builder name(String name) {
            this.name = name;
            return this;
        }
        public Builder age(int age) {
            this.age = age;
            return this;
        }
        public User build(){
            User user = new User(this);
            if(name == null)
                throw new RuntimeException("name can not be null");
            if(age < 0)
                throw new RuntimeException("age can not be less than 0");
            return user;
        }
    }
}
